#! /usr/bin/python
"""
Provides methods that can be used to crack vaults secured with passwords
that can be found in the dictionary.

Along with the dictionary words there are some other words included that
are common passwords.
"""
import sys, os
sys.path.insert(0, "..")
import time
from Vault import Vault

def crack(vault):
    """
    Attempts to break into a vault using a dictionary attack
    Times the attempt and returns the guessed value and time taken
    
    Usage: (guess, data, time) = crack(vault)
    """
    tick = time.time()
    data = None
    path = "../wordlists/"
    listing = os.listdir(path)
    for name in listing:
        dictionary = open(path + name)
        for guess in dictionary:
            guess = guess.strip()
            try:
                data = vault.open(guess)
                dictionary.close()
                break
            except:
                pass
        else:
            dictionary.close()
            continue
        break
    if data == None:
        raise Exception("Could not crack password:")
    tock = time.time()
    return (guess, data, (tock - tick))
        
if __name__ == "__main__":
    v = Vault("password", "This is super secret information")
    (password, data, cracktime) = crack(v)
    print "I got it! Your key was %s" % password
    print "The vault contained:"
    print "    %s" % data
    print "It took %f seconds to break" % cracktime
